package com.yiduo.outpost.core.model.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@Schema(description = "通用响应类")
public class Result<T> {

    @Schema(description = "响应业务码", required = true)
    private Integer code;

    @Schema(description = "是否成功", required = true)
    private boolean success;

    @Schema(description = "提示信息", required = true)
    private String message;

    @Schema(description = "响应数据")
    private T data;

    private Result(Integer code, String message, T data) {
        this.success = ResultCodeEnum.SUCCESS.getCode() == code;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 自定义响应业务码
     *
     * @param ResultCodeEnum
     * @param data
     */
    private Result(ResultCodeEnum ResultCodeEnum, T data) {
        this(ResultCodeEnum.getCode(), ResultCodeEnum.getMessage(), data);
    }

    /**
     * 成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> ok(T data) {
        return new Result<>(ResultCodeEnum.SUCCESS, data);
    }

    public static Result ok() {
        return new Result<>(ResultCodeEnum.SUCCESS, null);
    }

    /**
     * 失败时候的调用
     */
    public static <T> Result<T> error(ResultCodeEnum ResultCodeEnum) {
        return new Result<>(ResultCodeEnum.getCode(), ResultCodeEnum.getMessage(), null);
    }

    public static <T> Result<T> error(ResultCodeEnum ResultCodeEnum, T data) {
        return new Result<>(ResultCodeEnum.getCode(), ResultCodeEnum.getMessage(), data);
    }
    public static <T> Result<T> error(Integer code, String message) {
        return new Result<>(code, message, null);
    }


}
